<template>
  <avue-crud :class="type=='view'?'top15':''" :option="option" :table-loading="loading" :data="data" ref="crud" v-model="form" @row-update="addUpdate" @row-del="rowDel" @row-save="rowSave" @refresh-change="refreshChange" @on-load="onLoad">
    <template slot="menuLeft">
      <el-button type="text" icon="el-icon-plus" class="rightbtn" @click="addRow">新增</el-button>
    </template>
  </avue-crud>
</template>
<script>
import { init } from '@/mixins/init';
import { numParseReg, codeReg } from '@/util/zhengze';

export default {
  mixins: [init],
  props: {
    projectId: {
      type: String,
    },
    productId: {
      type: String,
    },
    materialName: {
      type: String,
    },
    type: {
      type: String,
    },
    sopform: {
      type: Object,
    },
    data: {
      type: Array,
    },
    taskStartDate: {
      type: String,
    },
    taskEndDate: {
      type: String,
    },
  },
  data() {
    return {
      form: {},
      procedureOptions: [],
      customerDrawingNo: '',
      customerName: '',
      produceShopName: '',
      equipmentList: [],
      option: {
        index: true,
        indexLabel: '序号',
        // menu: false,
        menuWidth: 130,
        addBtn: false,
        editBtn: false,
        // delBtn: false,
        addRowBtn: false,
        cellBtn: true,
        column: [
          {
            label: '编码',
            prop: 'materialNo',
            minWidth: 150,
            overHidden: true,
            cell: true,
            maxlength: 20,
            rules: [
              {
                required: true,
                message: '请输入编码',
                trigger: 'blur',
              },
              {
                pattern: codeReg,
                message: '编码由字母、数字和“-”组成',
                trigger: 'blur',
              },
            ],
          },
          {
            label: '名称',
            prop: 'materialName',
            minWidth: 150,
            overHidden: true,
            cell: true,
            maxlength: 15,
            rules: [
              {
                required: true,
                message: '请输入名称',
                trigger: 'blur',
              },
            ],
          },
          {
            label: '规格',
            prop: 'specifications',
            minWidth: 150,
            overHidden: true,
            cell: true,
            maxlength: 30,
          },
          {
            label: '型号',
            prop: 'model',
            minWidth: 150,
            overHidden: true,
            cell: true,
            maxlength: 30,
          },
          {
            label: '制造数量',
            prop: 'purchaseNum',
            minWidth: 120,
            type: 'number',
            minRows: 1,
            maxRows: 99999,
            precision: 0,
            cell: true,
          },
          {
            label: '客户财产标识',
            prop: 'isCustomProp',
            minWidth: 120,
            cell: true,
            type: 'select',
            dicUrl: '/api/blade-system/dict/dictionary?code=yes_no',
            props: {
              label: 'dictValue',
              value: 'dictKey',
            },
          },
          {
            label: '计划开始日期',
            prop: 'planStartDate',
            type: 'date',
            valueFormat: 'yyyy-MM-dd',
            minWidth: 150,
            cell: true,
            pickerOptions: {
              disabledDate(time) {
                return;
              },
            },
            rules: [
              {
                required: true,
                message: '请选择计划开始日期',
                trigger: 'blur',
              },
            ],
          },
          {
            label: '计划结束日期',
            prop: 'planEndDate',
            type: 'date',
            valueFormat: 'yyyy-MM-dd',
            minWidth: 150,
            cell: true,
            pickerOptions: {
              disabledDate(time) {
                return;
              },
            },
            rules: [
              {
                required: true,
                message: '请选择计划结束日期',
                trigger: 'blur',
              },
            ],
          },
          {
            label: '实际开始日期',
            prop: 'actualStartDate',
            type: 'date',
            valueFormat: 'yyyy-MM-dd',
            minWidth: 150,
            cell: true,
            pickerOptions: {
              disabledDate(time) {
                return;
              },
            },
          },
          {
            label: '实际完成日期',
            prop: 'actualEndDate',
            type: 'date',
            valueFormat: 'yyyy-MM-dd',
            minWidth: 150,
            cell: true,
            pickerOptions: {
              disabledDate(time) {
                return;
              },
            },
          },
          {
            label: '试模日期',
            prop: 'mouldTryoutDate',
            type: 'date',
            valueFormat: 'yyyy-MM-dd',
            minWidth: 150,
            hide: false,
            cell: true,
            pickerOptions: {
              disabledDate(time) {
                return;
              },
            },
          },
          {
            label: '验收日期',
            prop: 'mouldCheckDate',
            type: 'date',
            valueFormat: 'yyyy-MM-dd',
            minWidth: 150,
            hide: false,
            cell: true,
            pickerOptions: {
              disabledDate(time) {
                return;
              },
            },
          },
        ],
      },
    };
  },
  created() {
    if (this.type == 'edit') {
      this.option.header = true;
      this.option.menu = true;
    } else {
      this.option.header = false;
      this.option.menu = false;
    }
    this.findObject(this.option.column, 'planStartDate').pickerOptions.disabledDate = (time) => {
      return (
        time.getTime() < new Date(this.taskStartDate).getTime() - 86400000 ||
        time.getTime() > new Date(this.taskEndDate).getTime()
      );
    };
    this.findObject(this.option.column, 'planEndDate').pickerOptions.disabledDate = (time) => {
      return (
        time.getTime() < new Date(this.taskStartDate).getTime() - 86400000 ||
        time.getTime() > new Date(this.taskEndDate).getTime()
      );
    };
    this.findObject(this.option.column, 'actualStartDate').pickerOptions.disabledDate = (time) => {
      return time.getTime() < new Date(this.taskStartDate).getTime() - 86400000;
    };
    this.findObject(this.option.column, 'actualEndDate').pickerOptions.disabledDate = (time) => {
      return time.getTime() < new Date(this.taskStartDate).getTime() - 86400000;
    };
    this.findObject(this.option.column, 'mouldTryoutDate').pickerOptions.disabledDate = (time) => {
      return time.getTime() < new Date(this.taskStartDate).getTime() - 86400000;
    };
    this.findObject(this.option.column, 'mouldCheckDate').pickerOptions.disabledDate = (time) => {
      return time.getTime() < new Date(this.taskStartDate).getTime() - 86400000;
    };
  },
  watch: {},
  methods: {
    addRow() {
      this.$refs.crud.rowCellAdd({
        purchaseNum: 1,
      });
    },
    rowSave(row, done, loading) {
      if (!numParseReg.test(row.purchaseNum)) {
        this.$message.error('制造数量请输入大于0的整数');
        loading();
        return;
      }
      if (row.planStartDate && row.planEndDate && new Date(row.planStartDate) > new Date(row.planEndDate)) {
        this.$message.error('计划开始日期不应晚于计划结束日期');
        loading();
        return;
      }
      if (row.actualStartDate && row.actualEndDate && new Date(row.actualStartDate) > new Date(row.actualEndDate)) {
        this.$message.error('实际开始日期不应晚于实际结束日期');
        loading();
        return;
      }
      this.data.push(row);
      done();
    },
    addUpdate(row, index, done, loading) {
      if (!numParseReg.test(row.purchaseNum)) {
        this.$message.error('制造数量请输入大于0的整数');
        loading();
        return;
      }
      if (row.planStartDate && row.planEndDate && new Date(row.planStartDate) > new Date(row.planEndDate)) {
        this.$message.error('计划开始日期不应晚于计划结束日期');
        loading();
        return;
      }
      if (row.actualStartDate && row.actualEndDate && new Date(row.actualStartDate) > new Date(row.actualEndDate)) {
        this.$message.error('实际开始日期不应晚于实际结束日期');
        loading();
        return;
      }
      this.data.splice(index, 1, row);
      done();
    },
    rowDel(row, index) {
      this.$confirm('确定将选择数据删除?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(() => {
        this.data.splice(index, 1);
      });
    },
  },
};
</script>
<style lang="scss">
.top15 {
  margin-top: 15px;
}
</style>